<template>
  <div>
    <el-dialog
      :title="cateForm.id ? '编辑' : '新增'"
      :visible.sync="is.show"
      :before-close="cancel"
    >
      <el-form :model="cateForm" ref="cateForm" :rules="rules">
        <!-- 上级分类 -->
        <el-form-item label="上级分类" prop="pid" :label-width="formLabelWidth">
          <el-select v-model="cateForm.pid" placeholder="请选择上级分类">
            <el-option label="顶级菜单" :value="0"></el-option>
            <el-option
              v-for="item in cateList"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <!-- 分类名称 -->
        <el-form-item
          label="分类名称"
          prop="catename"
          :label-width="formLabelWidth"
        >
          <el-input v-model="cateForm.catename" autocomplete="off"></el-input>
        </el-form-item>

        <!-- 上传图片 -->
        <el-form-item
          v-if="this.cateForm.pid != 0"
          label="图片"
          :label-width="formLabelWidth"
          prop="upImg"
        >
          <el-upload
            action=""
            :on-change="changeImg"
            list-type="picture-card"
            :auto-upload="false"
            :limit="1"
            :on-exceed="
              () => {
                $message.error('超出上传限制');
              }
            "
            :file-list="arr"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>

        <!-- 状态 -->
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="cateForm.status"
            active-color="#13ce66"
            inactive-color="#ccc"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="enter">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
//引入辅助性函数
import { mapState, mapActions } from "vuex";
//调用添加或编辑接口
import { addOreditCate, cateinfo } from "../../request/api";
export default {
  data() {
    //自定义验证规则
    const uploadImg = (a, b, callback) => {
      if (this.cateForm.img == "") {
        callback(new Error("请上传图片"));
      } else {
        callback();
      }
    };
    return {
      cateForm: {
        catename: "",
        pid: "",
        img: "",
        status: 1,
      },
      //为了回显
      arr: [],
      formLabelWidth: "120px",
      dialogVisible: false,
      //表单验证规则
      rules: {
        pid: [{ required: true, message: "请选择上级分类", trigger: "blur" }],
        catename: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
          { min: 2, max: 8, message: "请输入2-8个字符", trigger: "blur" },
        ],
        upImg: { required: true, validator: uploadImg, trigger: "blur" },
      },
    };
  },

  //计算属性 使用辅助性函数mapState
  computed: {
    ...mapState("cate", ["is", "cateList"]),
  },

  //方法 使用辅助性函数 mapActions
  methods: {
    ...mapActions("cate", ["getCate"]),
    //图片上传成功时的函数
    changeImg(file) {
      this.cateForm.img = file.raw;
    },
    //点击确认事件
    enter() {
      let url = this.cateForm.id ? "cateedit" : "cateadd";
      //验证是否通过
      this.$refs.cateForm.validate((res) => {
        if (res) {
          addOreditCate(url, this.cateForm, true).then((res1) => {
            if (res1.code == 200) {
              //显示添加成功
              this.$message.success(res1.msg);
              //关闭弹窗
              this.cancel();
              //重新获取数据
              this.getCate();
            } else {
              this.$message.error(res1.msg);
            }
          });
        }
      });
    },

    //点击取消事件
    cancel() {
      this.cateForm = {
        catename: "",
        pid: "",
        img: "",
        status: 1,
      };
      //清楚表单验证
      this.$refs.cateForm.resetFields();
      this.arr = [];
      //关闭列表
      this.$store.commit("cate/changeDialog", false);
    },

    //用来数据回显的方法
    dataForm(id) {
      //获取当前编辑的数据
      cateinfo({ id }).then((res) => {
        if (res.code == 200) {
          //数据回显  是否含有图片
          if (res.list.img) {
            //给图片回显
            this.arr = [{ url: res.list.img }];
          }
          this.cateForm = res.list;
          //设置id
          this.cateForm.id = id;
        }
      });
    },
  },
};
</script>


<style scoped>
</style>